<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebOJ - 流式AI聊天</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .chat-area {
            margin-top: 20px;
            display: flex;
            flex-direction: column;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        textarea, input, select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            box-sizing: border-box;
        }
        textarea {
            min-height: 100px;
            resize: vertical;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 15px;
            font-size: 16px;
            cursor: pointer;
            border-radius: 4px;
            display: block;
            margin-top: 10px;
        }
        button:hover {
            background-color: #45a049;
        }
        #response {
            margin-top: 20px;
            padding: 15px;
            background-color: #f9f9f9;
            border: 1px solid #ddd;
            border-radius: 5px;
            min-height: 100px;
            white-space: pre-wrap;
        }
        .status {
            font-style: italic;
            color: #888;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>WebOJ - 流式AI聊天</h1>
        
        <div class="chat-area">
            <div class="form-group">
                <label for="message">问题/消息:</label>
                <textarea id="message" placeholder="输入你的问题..."></textarea>
            </div>
            
            <div class="form-group">
                <label for="code">代码 (可选):</label>
                <textarea id="code" placeholder="输入相关代码..."></textarea>
            </div>
            
            <div class="form-group">
                <label for="errorMessage">错误信息 (可选):</label>
                <input type="text" id="errorMessage" placeholder="如有错误消息，请输入...">
            </div>
            
            <div class="form-group">
                <label for="language">编程语言 (可选):</label>
                <select id="language">
                    <option value="">选择语言</option>
                    <option value="Java">Java</option>
                    <option value="Python">Python</option>
                    <option value="JavaScript">JavaScript</option>
                    <option value="C++">C++</option>
                    <option value="C#">C#</option>
                </select>
            </div>
            
            <button id="sendBtn">发送</button>
            
            <div class="status" id="status">未连接</div>
            
            <label for="response">AI响应:</label>
            <div id="response"></div>
        </div>
    </div>

    <script>
        // WebSocket连接
        let socket;
        let connected = false;
        const statusElement = document.getElementById('status');
        const responseElement = document.getElementById('response');
        
        // 建立WebSocket连接
        function connect() {
            const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
            const wsUrl = `${wsProtocol}//${window.location.host}/ai/stream-chat`;
            
            socket = new WebSocket(wsUrl);
            
            socket.onopen = function() {
                connected = true;
                statusElement.textContent = '已连接';
                statusElement.style.color = 'green';
            };
            
            socket.onclose = function() {
                connected = false;
                statusElement.textContent = '连接已关闭';
                statusElement.style.color = 'red';
            };
            
            socket.onerror = function(error) {
                console.error('WebSocket错误:', error);
                statusElement.textContent = '连接错误';
                statusElement.style.color = 'red';
            };
            
            socket.onmessage = function(event) {
                try {
                    const response = JSON.parse(event.data);
                    
                    if (!response.success) {
                        responseElement.innerHTML += `<p style="color: red;">错误: ${response.error}</p>`;
                        return;
                    }
                    
                    if (response.content) {
                        responseElement.innerHTML += response.content;
                    }
                    
                    if (response.done) {
                        statusElement.textContent = '回答完成';
                    }
                } catch (e) {
                    console.error('解析消息时出错:', e);
                    responseElement.innerHTML += `<p style="color: red;">解析响应时出错: ${e.message}</p>`;
                }
            };
        }
        
        // 发送消息
        document.getElementById('sendBtn').addEventListener('click', function() {
            // 清空之前的响应
            responseElement.innerHTML = '';
            statusElement.textContent = '处理中...';
            
            if (!connected) {
                connect();
                // 稍微延迟发送，确保连接已建立
                setTimeout(sendMessage, 500);
            } else {
                sendMessage();
            }
        });
        
        function sendMessage() {
            const message = document.getElementById('message').value.trim();
            const code = document.getElementById('code').value.trim();
            const errorMessage = document.getElementById('errorMessage').value.trim();
            const language = document.getElementById('language').value;
            
            const requestData = {
                message: message || null,
                code: code || null,
                errorMessage: errorMessage || null,
                language: language || null
            };
            
            try {
                socket.send(JSON.stringify(requestData));
            } catch (error) {
                console.error('发送消息时出错:', error);
                responseElement.innerHTML = `<p style="color: red;">发送消息时出错: ${error.message}</p>`;
            }
        }
        
        // 页面加载时连接
        window.addEventListener('load', connect);
    </script>
</body>
</html>